package io.bdmc.modules.bss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.bdmc.common.annotation.DataFilter;
import io.bdmc.common.utils.MyPage;
import io.bdmc.modules.bss.entity.DeviceAlarmRealtime;
import io.bdmc.modules.bss.mapper.DeviceAlarmRealtimeMapper;
import io.bdmc.modules.bss.model.DeviceAlarmParam;
import io.bdmc.modules.bss.model.DeviceAlarmRealtimeParam;
import io.bdmc.modules.bss.service.IDeviceAlarmRealtimeService;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 设备报警历史记录 服务实现类
 * </p>
 *
 * @author 黄露
 * @since 2020-09-15
 */
@Service
public class DeviceAlarmRealtimeServiceImpl extends ServiceImpl<DeviceAlarmRealtimeMapper, DeviceAlarmRealtime>
        implements IDeviceAlarmRealtimeService {
    @Autowired
    DeviceAlarmRealtimeMapper _alarmRealtimeService;

    @DataFilter(tableAlias = "d")
    @Override
    public MyPage<DeviceAlarmRealtime> mySelectPage(MyPage<DeviceAlarmRealtime> myPage,
            DeviceAlarmRealtimeParam param) {
        return _alarmRealtimeService.mySelectPage(myPage, param);
    }

    @Override
    public MyPage<DeviceAlarmRealtime> mySelectPagePermitAll(MyPage<DeviceAlarmRealtime> myPage,
            DeviceAlarmRealtimeParam param) {
        return _alarmRealtimeService.selectAllLastAlarm(myPage, param);
    }

    @Override
    public List<DeviceAlarmRealtime> selectAllLastAlarm(DeviceAlarmRealtimeParam param) {
        return _alarmRealtimeService.selectAllLastAlarm1(param);
    }

    @Override
    public DeviceAlarmRealtime getLastOneExistAlarm(String deviceSn, String alarmTypeCode, Integer channel) {
        LambdaQueryWrapper<DeviceAlarmRealtime> w1 = new LambdaQueryWrapper<>();
        w1.eq(DeviceAlarmRealtime::getDeviceSn, deviceSn).eq(DeviceAlarmRealtime::getAlarmTypeCode, alarmTypeCode)
                .last("LIMIT 1");
        if (channel != null && channel >= 0) {
            w1.eq(DeviceAlarmRealtime::getChannel, channel);
        }
        return _alarmRealtimeService.selectOne(w1);
    }

    @Override
    public List<DeviceAlarmRealtime> selectRealtimeAlarmList() {
        return _alarmRealtimeService.selectRealtimeAlarmList();
    }

}
